<?php /**/
/**
 * @version 1.0.5
 * @author tuadmin
 * @copyright 2012
 * @file mytemplate
 * + fix error Eval
 * + add functioon _showDebug
 * --1.0.4--
 * + fix control for existence of dirs
 * --1.0.3--
 * + add function refs array by reference ->refs ($array)
 * + add function ref variables by reference ->ref('alias',$variable)
 * --1.0.2--
 * + add support for multipath,array() or string PATH
 * + add function concatAssing 
 * + add function concatVar
 * --1.0.1--
 * + add Comunicators templates variables $__URL__ or $this->URL
 * + add static function newInstance()
 */
if(!defined('MODE_DEBUG')){define('MODE_DEBUG',true);} if(@class_exists('MyErrorException')){class myTemplateException extends MyErrorException{}} else{ class myTemplateException extends Exception{ public function __construct($a, $b, $c, $d, $e = null) { parent::__construct($a, $b); } public function showDebug(){} }} class myTemplate { private $f = array(); private $g = array(); private $h; private $i = '.'; private $j = array(); private $k = array(); private $l = '.tpl.php'; private $m = 0; private $n = array(); private $o = array(); private $p = ''; private $q = false; const DEBUG = MODE_DEBUG; static public function newInstance($r ,$s = false) { return new self($r ,$s); } public function __construct($r ,$s = false) { $this->a($r); $this->l = $s? $s : $this->l; } private function a($r) { if(is_array($r)) { $this->k = array(); $this->q = true; if(isset($r[0])){ foreach($r as $t ) { if(!is_dir($t)){continue;} $this->k[] = $t; $this->j[] = '.'; } }else { foreach($r as $t => $u) { if(!is_dir($t)){continue;} $this->k[] = $t; $this->j[] = $u; } } } $this->h = $r; } public function setUrl($u) { $this->i = $u; } public function vars($v) { $this->f[$this->m] = $v; return $this; } public function _($w,$x) { $this->f[$this->m][$w] = $x; return $this; } public function assigns($v) { $this->g[$this->m] = $v; return $this; } public function assign($w,$x) { $this->g[$this->m][$w] = $x; return $this; } public function & scope($v) { if(is_string($v)) { $v = func_get_args(); } $this->g[$this->m] = array_fill_keys($v,null); return $this->g[$this->m]; } public function ref($w,& $x) { $this->g[$this->m][$w] =& $x; return $this; } public function refs(&$v) { $this->g[$this->m]= & $v; return $this; } public function concatAssign($w,$x) { if(isset($this->g[$this->m][$w]) && is_string($this->g[$this->m][$w]) ) { $this->g[$this->m][$w] .= $x; } else { $this->g[$this->m][$w] = $x; } return $this; } public function concatVar($w,$x) { if(isset($this->f[$this->m][$w]) && is_string($this->f[$this->m][$w])) { $this->f[$this->m][$w] .= $x; } else { $this->f[$this->m][$w] = $x; } return $this; } public function & getData($y = null,$z = false) { $A = false; if($y == null) { return $this->g[$this->m]; } else { if(isset($this->g[$this->m][$y])) { return $this->g[$this->m][$y]; } else if($this->f[$this->m][$y]) { return $this->f[$this->m][$y]; } } return $A; } public function & getAllData($B = true) { $A = false; if($B) { return $this->g[$this->m]; } else { return $this->f[$this->m]; } return false; } public function __set($w,$x) { $this->assign($w,$x); } public function __call($C,$D) { $y = array_shift($D); switch(strtolower($C)) { case 'getdata' : return isset($this->f[$y]) ? $this->f[$y] : $this->g[$y]; } return false; } public function set($w,$x) { is_string($x) ? $this->_($w,$x) : $this->assign($w,$x); return $this; } public function __get($E) { switch(true) { case isset($this->f[$this->m][$E]):return $this->f[$E]; case isset($this->g[$this->m][$E]):return $this->g[$E]; default:false; } } public function erase($F = 'all') { switch($F) { default : ; case 'all' : $this->f[$this->m] = array(); case 'assigns' : $this->g[$this->m] = array(); break; case 'vars' : $this->f[$this->m] = array(); break; } return $this; } public function parse($G,$H = false) { $this->m++; $this->n[$this->m] = $G ; $this->f[$this->m] = array(); $this->g[$this->m] = array(); if($H && !isset($this->o[$G])) { $r = $this->__return_path_file($G); $this->o[$G] = $r? file_get_contents($r) :false; } return $this; } public function render($I = false) { if(self::DEBUG ) { try { return self::DEBUG ? $this->render_debug_on($I) : $this->render_debug_off($I); }catch(Exception $J){echo $J->getMessage() . $J->getLine(). $J->getFile();} } else { return $this->render_debug_off($I); } } protected function render_debug_on($I) { ob_start (); $__NAME__ = & $this->n[$this->m]; if(isset($this->o[ $__NAME__])) { $__ERROR__ = is_string($this->o[ $__NAME__]) ? $this->load_memory() :$this->_set_error('file'); } else { $__FILE__ = $this->__return_path_file($__NAME__); $__ERROR__ = $__FILE__? $this->load_disk_debug_on($__FILE__) :$this->_set_error('file'); } if($__ERROR__) { throw new Exception("\nTemplate not Found :" . $this->h . DIRECTORY_SEPARATOR . $__NAME__ .$this->l,404); return false; } return false !== $I ? $this->parser_vars(ob_get_clean(),$I).$this->reduce_level() : ob_get_clean().$this->reduce_level(); } protected function render_debug_off($I) { ob_start (); $__NAME__ = & $this->n[$this->m]; if(isset($this->o[ $__NAME__])) { $__ERROR__ = is_string($this->o[ $__NAME__]) ? $this->load_memory() :$this->_set_error('file'); } else { $__FILE__ = $this->__return_path_file($__NAME__); $__ERROR__ = $__FILE__? $this->load_disk($__FILE__) :$this->_set_error('file'); } if($__ERROR__) { return false; } return false !== $I ? $this->parser_vars(ob_get_clean(),$I).$this->reduce_level() : ob_get_clean().$this->reduce_level(); } protected function _set_error($K) { switch($K) { case 'file' :$L = 'template file not found'; break; case 'parse' :$L = 'template syntax error'; break; default:$L = 'Unknown Error'; } $this->p = $L; return true; } protected function load_memory() { extract($this->f[$this->m],EXTR_REFS); extract($this->g[$this->m],EXTR_REFS); $__URL__ = & $this->i; $__PATH__ = & $this->h; try{ if(@eval ( '?>' . $this->o[ $this->n[$this->m] ] ) === false && self::DEBUG) { $M = error_get_last(); throw new myTemplateException($M['message'], $M['type'], $__FILE__, $M['line']); $this->_set_error('parse'); } }catch(myTemplateException $J) {die($J->showDebug());} } protected function reduce_level() { $this->m--; } protected function load_disk($__FILE__) { extract($this->f[$this->m],EXTR_REFS); extract($this->g[$this->m],EXTR_REFS); $__URL__ = & $this->i; $__PATH__ = & $this->h; include $__FILE__; } protected function load_disk_debug_on($__FILE__) { extract($this->f[$this->m],EXTR_REFS); extract($this->g[$this->m],EXTR_REFS); $__URL__ = & $this->i; $__PATH__ = & $this->h; $__contenido__ = file_get_contents($__FILE__); try{ if(@eval ( '?>' . $__contenido__ ) === false) { $M = error_get_last(); throw new myTemplateException($M['message'], $M['type'], $__FILE__, $M['line']); }}catch(myTemplateException $J) {die($J->showDebug());} } protected function parser_vars($N,$O = true) { $P = array(); if(is_array($O)) { foreach( $O as $Q => $x) { $P['{'.$Q.'}'] = & $O[$Q]; } } else { foreach( $this->f[$this->m] as $Q => $x) { $P['{'.$Q.'}'] = & $this->f[$this->m][$Q]; } } return strtr($N , $P); } protected function __return_path_file($__NAME__) { if($this->q) { foreach($this->k as $Q => $h) { if(file_exists($h . DIRECTORY_SEPARATOR . $__NAME__ .$this->l)) { $this->i = $this->j[$Q]; $this->h = $this->k[$Q]; return $h . DIRECTORY_SEPARATOR . $__NAME__ .$this->l; } else { $this->i = $this->j[$Q]; $this->h = $this->k[$Q]; } } } else { return realpath($this->h . DIRECTORY_SEPARATOR . $__NAME__ .$this->l); } return false; } }